<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" custom :labels="labels" @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form-item>
                        <div class="data-select">
                            <DmData @dataChange="dataChange" />
                            <Upload @uploadSuccess="uploadSuccess" />
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleCalculate">计算</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>对比基期（基准时期）和报告期（分析时期）各部分的占比变化，计算变动幅度（变化指数报告期占比基期占比），整体指数为各部分变化的平均值。</p>

                <h5>使用范围</h5>
                <ul>
                    <li>分析数据结构的动态变化（如两年内各产品营收占比的变动）；</li>
                    <li>识别结构调整的关键部分（如原材料成本占比上升最快）；</li>
                    <li>适用于跨时期的结构对比。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :result="resultData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import {structureChange}             from "@/api/structure"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

let labels = ["组成名称", "基期", "报告期"]

function change(list) {
    paramsData.value.componentNames     = list?.find(item => item.label === "组成名称")?.value || []
    paramsData.value.basePeriodValues   = list?.find(item => item.label === "基期")?.value || []
    paramsData.value.reportPeriodValues = list?.find(item => item.label === "报告期")?.value || []
}

let paramsData = ref({
    componentNames    : [],
    basePeriodValues  : [],
    reportPeriodValues: []
})

let resultShow = ref(false)
let resultData = ref("")

// 计算
function handleCalculate() {
    if (paramsData.value.componentNames.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }
    if (paramsData.value.basePeriodValues.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }
    if (paramsData.value.reportPeriodValues.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }

    structureChange(paramsData.value).then((res: any) => {
        resultData.value = res?.data
        resultShow.value = true
    })
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("structure-change").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>

</style>

